import ossClient from './aliyun-oss-sdk-4.4.4.min.js'
export default{
	getPubkey:function(vue){
		  return vue.$http.get(vue.utils.getServiceUrl()+'/sys/pubkey',{})
		    .then(function(res){
		      if (res.data.code=="001"){
		        vue.publicKey = res.data.result.data;
		      }else{
		        alert(res.data.msg);
		      }
		  })
	},
	isNull:function(str){
			if(str.replace(/(^\s*)|(\s*$)/g, "" ) == ""){
				return true;
			}else{
				return false;
			}
	},
	uploadFile:function (cnt,fileId,pic,tishi) {
	  var _self = cnt;
	  var url = '';
    var random = parseInt(Math.random()*(1000-1+1)+1000);//添加随机数
	  var caution = document.getElementById(tishi);
	  caution.textContent='上传中';
	  caution.style.color='#3b7bea'
	  return ossClient.urllib.request(window.location.host+'/nb-bs/api/upload/getOssStsToKen', {
	    method: 'post'
	  }).then(function (result) {
	    var creds = JSON.parse(result.data);
      var aliyun = creds.result.data.domain; //
      var client = new ossClient.Wrapper({
	      region: 'oss-cn-shenzhen',
	      accessKeyId: creds.result.data.accessKeyId,
	      accessKeySecret: creds.result.data.accessKeySecret,
	      stsToken: creds.result.data.securityToken,
	      bucket: creds.result.data.bucket
	    });
	    var file = document.getElementById(fileId).files[0];
	    var key = _self.formatDate(new Date(),'yyyyMMdd')+'/'+_self.formatDate(new Date(),'yyyyMMddhhmmss')+random;
	    return client.multipartUpload(key, file, {

	    }).then(function (res) {
	      caution.textContent='上传成功';
	      caution.style.color='#3b7bea'
	      document.getElementById(pic).src=aliyun+res.name;
	    }).catch(function (err) {
	      caution.textContent='上传失败';
	      caution.style.color='red'
	    });
	  }).catch(function (err) {
	    caution.textContent='上传失败';
	    caution.style.color='red'
	  });
	},
	setString:function(key,value){
		// ss.setItem(key,value);
	},
	getString:function(key){
		// return ss.getItem(key);
	},
	setObject:function(key,json){
		// ss.setItem(key,JSON.stringify(json));
	},
	getObject:function(key){
		// return JSON.parse(ss.getItem(key));
	},
	getAliOos:function(){
		return 'http://kp-test.oss-cn-shenzhen.aliyuncs.com/';
	},
	getServiceUrl:function(){
		return "/nb-bs/api";
	}

}
